package xx.ring.dept.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xx.ring.db.DBHelper;
import xx.ring.dept.domain.Dept;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(("/dept"))
public class DeptController {
    private DBHelper db=new DBHelper();

    @GetMapping("/{view}")
    String view(@PathVariable("view") String view) {
        return "dept/"+view;
    }

    @ResponseBody
    @GetMapping("/get")
    Object get() {
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        List<Dept> depts = new ArrayList<>();
        try {
            conn = db.getConn();
            String sql = "select id,name,pid from dept";
            pstat = conn.prepareStatement(sql);
            rs = pstat.executeQuery();

            while (rs.next()) {
                Dept dept=new Dept(rs.getString("id"),rs.getString("name"),rs.getString("pid"));
                depts.add(dept);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.closeConn(conn, pstat, rs);
        }
        return depts;
    }
}
